<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">备件编码：</div>
          <el-input
            v-model="sparePartsCode"
            clearable
            placeholder="请输入备件名称"
            @change="queryPageList()"
          />
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">备件名称：</div>
          <el-input
            v-model="sparePartsName"
            clearable
            placeholder="请输入备件名称"
            @change="queryPageList()"
          />
        </div>
      </div>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :has-add="false"
        :has-del="false"
        :page-no="164"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="sparePartsCode"
        :showSelectColumn="false"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="createTime"
          v-if="scoped.row.sparePartsCode"
          slot-scope="scoped"
        >
        {{dateToFormatTwo(scoped.row.lastUserDate, 'YYYY年MM月DD日')}}
        </template>
        <template
          slot="operate"
          v-if="scoped.row.sparePartsCode"
          slot-scope="scoped"
        >
          <span
            class="table-btns-box"
            @click="check(scoped.row)"
          >
            <i class="el-icon-view"></i>
            <span>查看使用记录</span>
          </span>
        </template>
      </e-table>

      <AddEditDialog
        ref="addEditDialog"
        :equ-model-arr="equModelArr"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import sparePartsApi from "@/api/basic-info-manage/spare-parts-manage";
export default {
  name: "SparePartsUsageStatistics",
  components: { ActionBar, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 所搜条件：关键字名称
      sparePartsName: "",
      //  搜索条件:查询条件
      sparePartsCode: "",
      // 设备型号数组
      equModelArr: [],
      checkedIdList: [],
      tableLabel: [
        {
          label: "备件编码",
          param: "sparePartsCode",
          show: true,
        },
        {
          label: "备件名称",
          param: "sparePartsName",
          show: true,
        },
        {
          label: "累计使用量",
          param: "total",
          show: true,
          notSearch: true,
        },
        {
          label: "最近使用日期",
          param: "lastUserDate",
          show: true,
          slotName: "createTime",
          notSearch: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {
    tableLabel: {
      handler(v) {
        console.log("tableLabel watch", v);
      },
      immediate: true,
    },
  },
  created() {
    this.queryPageList();
  },
  mounted() {},
  methods: {
    // 清空选项
    cancelSelect(data) {
      if (data === "equModelCode") {
        this.equModelCode = "";
      }
      this.queryPageList();
    },
    // 选择
    selectEquModelCode(id) {
      if (this.equModelCode === id) {
        this.equModelCode = "";
      } else {
        this.equModelCode = id;
      }
      this.queryPageList();
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          sparePartsName: this.sparePartsName || void 0,
          sparePartsCode: this.sparePartsCode || void 0,
          ...e,
        },
      };
      sparePartsApi.queryInfoPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.keywordCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.keywordCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    // 修改
    check(row) {
      if (row.sparePartsCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "查看";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .el-collapse-item__content {
  padding-bottom: 10px;
}
</style>
